class StoneMap extends egret.DisplayObjectContainer {
	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
	}

	private stageW: number;
	private stageH: number;
	private canPassStoneDis: number[] = [];
	private notPassStoneDis: number[] = [];
	private canPassStone: Stone[] = [];
	private notPassStone: Stone[] = [];
	private downCanPassStone:number = 0;
	private canPassStoneDisArr:any = {};

	private onAddToStage() {
		this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
		this.stageW = this.stage.stageWidth;
		this.stageH = this.stage.stageHeight;
		this.createMap();
	}
	public destroy(){
		this.canPassStoneDis = null;
		this.notPassStoneDis = null;
		this.canPassStone = null;
		this.notPassStone = null;
		this.canPassStoneDisArr = null;
	}
	public getDownCanPassStone():number {
		return this.downCanPassStone;
	}
	public getCanPassStone(): Stone[] {
		return this.canPassStone;
	}

	public getCanPassStoneDis(): number[] {
		return this.canPassStoneDis;
	}

	private AddCanPassStoneDis() {
		let i: number = Math.round(Math.random() * 1);
		
		this.canPassStoneDis.push(i);
	}

	private AddNotPassStoneDis():number {
		let i: number = Math.round(Math.random() * 10);
		let j: number = 0;
		if (i <= 5) {
			j = 0;
		}
		if (i > 5 && i <= 7) {
			j = 1;
		}
		if (i > 7 && i <= 9) {
			j = 2;
		}
		if (i > 9 && i <= 10) {
			j = 3;
		}
		return j;
		//this.notPassStoneDis.push(j);
	}

	private createMap() {
		let stoneNum: number = 6;
		for (var i = 0; i < stoneNum; i++) {
			this.addStoneAndDis();
		}
	}
	private lastX:number=0;
	private lastY:number=0;

	public mapMove() {
		this.removeStoneAndDis();
		this.addStoneAndDis();
		let stoneW: number = this.canPassStone[0].width;
		let stoneH: number = this.canPassStone[0].height;
		let dis: number = this.canPassStoneDis[0] ? -1 : 1;
		this.lastX +=dis * (stoneW / 2);
		this.lastY +=(stoneH - 36);
	
		egret.Tween.removeTweens(this.parent);
		egret.Tween.get(this.parent).to({ x: this.lastX, y: this.lastY }, 800)
	}
	
	public getCanPassStoneDisArr(){
		return this.canPassStoneDisArr;
	}
	private curNum:number = 0;
	private addStoneAndDis() {
		let passStone: Stone = StonePool.getObjectPool(0);
		this.addChildAt(passStone, 0);
		
		if (this.canPassStone[0] == null) {
			passStone.x = this.stageW - passStone.width >> 1;
			passStone.y = this.stageH - (300 + passStone.height);
			//egret.Tween.get(passStone).to({y: this.stageH - (300 + passStone.height)}, 150);
			
			//this.canPassStoneDisArr[this.curNum] = -1;
			this.canPassStoneDisArr[this.curNum] = {"dis":1,"stone":passStone}
			this.canPassStoneDis.push(-1);
		} else {
			this.AddCanPassStoneDis();
			let dis: number = this.canPassStoneDis[this.canPassStoneDis.length - 1] ? 1 : -1;
			//this.canPassStoneDisArr[this.curNum] = this.canPassStoneDis[this.canPassStoneDis.length - 1];
			this.canPassStoneDisArr[this.curNum] = {"dis":this.canPassStoneDis[this.canPassStoneDis.length - 1],"stone":passStone}
			passStone.x = this.canPassStone[this.canPassStone.length - 1].x + dis * (passStone.width / 2);
			let y =this.canPassStone[this.canPassStone.length - 1].y - (passStone.height - 36)
			
			if (this.curNum>6){
				passStone.y = y-100;
				egret.Tween.get(passStone).to({y:y}, 100);
			}else{
				passStone.y = y;
			}
			
		}
		this.curNum++;
		this.canPassStone.push(passStone);

		if (this.canPassStoneDis.length >= 2) {
			let notNum = this.AddNotPassStoneDis();
			let j = 1;
			let dist: number = this.canPassStoneDis[this.canPassStoneDis.length - 1] ? -1 : 1;
			var notPassStone: Stone;
			for (j; j <= notNum; j++) {
				notPassStone = StonePool.getObjectPool(Math.floor(Math.random() * 4 + 1))//new Stone(Math.floor(Math.random() * 4 + 1));
				this.addChildAt(notPassStone, 0);
				notPassStone.x = this.canPassStone[this.canPassStone.length - 2].x + dist * (passStone.width / 2) * (j);
				let y = this.canPassStone[this.canPassStone.length - 2].y - (passStone.height - 36) * (j);
				if (this.curNum>6){
					notPassStone.y = y-100;
					egret.Tween.get(notPassStone).to({y:y}, 100);
				}else{
					notPassStone.y = y;
				}
				this.notPassStone.push(notPassStone);
				
			}
		}
	}
	private DownPassStone: Stone[][] = [];

	private removeStoneAndDis() {
		let stone: Stone = this.canPassStone[0];
		let stoneY: number = stone.y;
		//this.canPassStone.splice(0, 1);
		this.canPassStoneDis.splice(0, 1);
		//egret.Tween.removeTweens(this.parent);
		
		
	}
	public AutoRemoveStoneAndDis2(){
		let i = 0;
		let dwonStones:Stone[];
		dwonStones = this.DownPassStone.shift()
		if (dwonStones!==undefined){
			for (i; i < dwonStones.length; i++) {
				dwonStones[i].stoneDown();
			}
		}else{
			let stone: Stone = this.canPassStone[0];
			let stoneY: number = stone.y;
			this.canPassStone.splice(0, 1);
			this.canPassStoneDis.splice(0, 1);
			stone.stoneDown();
			this.dispatchEventWith("gameOver")
		}
	}
	
	public AutoRemoveStoneAndDis(){
		let canPassStoneLength = this.canPassStone.length;
		if (canPassStoneLength>=10){
			let diffNum = canPassStoneLength-10;
			egret.log("diffNum==>",diffNum);
			let i=0;
			for (i;i<=diffNum;i++){
				let stone = this.canPassStone[0];
				let stoneY = stone.y;
				this.canPassStone.splice(0, 1);
				this.removeNotPassStone(stoneY);
				stone.stoneDown();
				this.downCanPassStone++;
			}
		}else{
			let stone = this.canPassStone[0];
			let stoneY = stone.y;
			this.canPassStone.splice(0, 1);
			this.removeNotPassStone(stoneY);
			stone.stoneDown();
			this.downCanPassStone++;
		}
	}
	private removeNotPassStone(stoneY:number){
		let i= 0;
		let notPassStone:Stone;
		for (i; i < this.notPassStone.length; i++) {
			if (this.notPassStone[i].y >= stoneY) {
				notPassStone = this.notPassStone[i];
				this.notPassStone.splice(0, 1);
				//this.notPassStoneDis.splice(0, 1);
				notPassStone.stoneDown();
			}
		}
	}
}